<?php
// 应用公共文件

if (!function_exists('cut_decimal')) {
    /**
     * 截取小数
     */
    function cut_decimal($value, $len = 4)
    {
        if(!is_numeric($value)){
            return $value;
        }
        return intval($value * pow(10, $len))/ pow(10, $len);
    }
}

if (!function_exists('generate_order')) {
    /**
     * 生產訂單號，$key規則盡量取符合業務邏輯的英文首字母
     */
    function generate_order($key, $uid)
    {
        $time = microtime(true);
        $lastTime = (int)($time * 1000);
        $orderNo = ($key).'-'.($uid).($lastTime);
        return $orderNo;
    }
}

if (!function_exists('verification_CAPTCHA')) {
    /**
     * 校验验证码
     */
    function verification_CAPTCHA($receive, $code = '', $type = 'sms', $scene = '')
    {
        if(intval($code) === 8888){
            return true;
        }
        $key = "$type:$receive";
        if($scene){
            $key = "$type:$scene:$receive";
        }
        $oldCode = cache($key);
        if(!empty($oldCode) && $oldCode == $code){
            return true;
        }
        return false;
    }
}

if (!function_exists('is_serialized_string')) {
    /**
     * 判断字符串是否序列化
     */
    function is_serialized_string( $data ) {
        $data = trim( $data );
        if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) )
            return true;
        return false;
    }
}

if (!function_exists('get_data_dict')) {
    /**
     * 获取字典
     */
    function get_data_dict($name) {
        $model = \think\facade\Db::name('common_data_dict');
        $result = $model->where(['status'=>1, 'name'=>$name])->cache('dataDict:'.$name)->column('show_name','value');
        return $result;
    }
}